﻿#region imports

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

using CondeNet.AssetManager.Data;
using CondeNet.AssetManager.Business;
using CondeNet.AssetManager.Business.Interface;

#endregion

namespace CondeNet.AssetManager.DataProvider.Sql.Mappers
{
    public static class RelatedAssetMapper
    {
        internal static IRelatedAsset ToBusinessObject ( T_RelatedAsset entity )
        {
            IRelatedAsset obj = new RelatedAsset (
                entity.RelatedAsset_IDPK ,
                entity.AssetID_FK.Value ,
                entity.RelatedAssetID_FK ,
                entity.StatusID_FK ,
                entity.CreationDate_DT ,
                entity.UpdatedDate_DT );

            // parent is causing recursion: obj.Asset = AssetMapper.ToBusinessObject ( entity.T_Asset );
            if ( entity.T_Asset1 != null ) obj.Related = AssetMapper.ToBusinessObject ( entity.T_Asset1 );
            if ( entity.T_Status != null ) obj.Status = StatusMapper.ToBusinessObject ( entity.T_Status );

            return obj;
        }

        internal static T_RelatedAsset ToEntity ( IRelatedAsset v )
        {
            T_RelatedAsset entity = new T_RelatedAsset ( );

            entity.RelatedAsset_IDPK = v.ID;
            entity.RelatedAssetID_FK = v.RelatedAssetID;
            entity.AssetID_FK = v.AssetID;
            entity.StatusID_FK = v.StatusID;
            entity.CreationDate_DT = v.CreatedDate;
            entity.UpdatedDate_DT = v.UpdatedDate;

            // parent is causing recursion: entity.T_Asset = AssetMapper.ToEntity ( v.Asset );
            if ( v.Related != null ) entity.T_Asset1 = AssetMapper.ToEntity ( v.Related );
            if ( v.Status != null ) entity.T_Status = StatusMapper.ToEntity ( v.Status );

            return entity;
        }

        internal static IList<IRelatedAsset> ToBusinessObjects ( IList<T_RelatedAsset> dalList )
        {
            return dalList.Select ( tmp => ToBusinessObject ( tmp ) ).ToList ( );
        }

        internal static IList<T_RelatedAsset> ToEntities ( IList<IRelatedAsset> entityList )
        {
            EntitySet<T_RelatedAsset> list = new EntitySet<T_RelatedAsset>();
            entityList.Select ( tmp => ToEntity ( tmp ) ).ToList ( ).ForEach(list.Add);
            return list;
        }
    }
}
